import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: { title: '首页', showTabBar: true }
  },
  {
    path: '/registration',
    name: 'Registration',
    component: () => import('../views/Registration.vue'),
    meta: { title: '预约挂号', requireAuth: true }
  },
  {
    path: '/medical-records',
    name: 'MedicalRecords',
    component: () => import('../views/MedicalRecords.vue'),
    meta: { title: '就诊记录', showTabBar: true, requireAuth: true }
  },
  {
    path: '/medical-record-detail/:id',
    name: 'MedicalRecordDetail',
    component: () => import('../views/MedicalRecordDetail.vue'),
    meta: { 
      title: '就诊详情',
      requireAuth: true
    }
  },
  {
    path: '/prescriptions',
    name: 'Prescriptions',
    component: () => import('../views/Prescriptions.vue'),
    meta: { title: '药方信息', requiresAuth: true }
  },
  {
    path: '/ward',
    name: 'Ward',
    component: () => import('../views/Ward.vue'),
    meta: { title: '我的病房', requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/Profile.vue'),
    meta: { title: '个人中心', showTabBar: true, requireAuth: true }
  },
  {
    path: '/change-password',
    name: 'ChangePassword',
    component: () => import('../views/ChangePassword.vue'),
    meta: { title: '修改密码', requireAuth: true }
  },
  {
    path: '/announcements',
    name: 'Announcements',
    component: () => import('../views/Announcements.vue'),
    meta: { title: '医院公告', showTabBar: true }
  },
  {
    path: '/announcement/:id',
    name: 'AnnouncementDetail',
    component: () => import('../views/AnnouncementDetail.vue'),
    meta: {
      title: '公告详情',
      requiresAuth: true
    }
  },
  {
    path: '/departments',
    name: 'Departments',
    component: () => import('../views/Departments.vue'),
    meta: { title: '科室导航', showTabBar: true }
  },
  {
    path: '/department/:id',
    name: 'DepartmentDetail',
    component: () => import('../views/DepartmentDetail.vue'),
    meta: { title: '科室详情' }
  },
  {
    path: '/doctor/:id',
    name: 'DoctorDetail',
    component: () => import('../views/DoctorDetail.vue'),
    meta: { title: '医生详情' }
  },
  {
    path: '/my-registrations',
    name: 'MyRegistrations',
    component: () => import('../views/MyRegistrations.vue'),
    meta: { title: '我的挂号', showTabBar: true, requireAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 医院移动端` : '医院移动端'
  
  // 检查是否需要登录认证
  if (to.meta.requireAuth) {
    const token = localStorage.getItem('token')
    if (!token && to.path !== '/login') {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
      return
    }
  }
  next()
})

export default router 